/**
 * AE Texture Object.
 *
 * Released under Apache License, Version 2.0.
 */

package bsp.andengine;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import android.content.Context;

public class AETextureObject
{
	protected Engine mEngine;
	protected Texture mTexture;
	protected boolean bCleanUpDone;
	
	public AETextureObject()
	{
		mEngine = null;
		mTexture = null;
		bCleanUpDone = false;
	}
	
	public void create(Engine engine, Context context, int wd, int ht, TextureOptions pTextureOptions)
	{
		mEngine = engine;
		mTexture = new Texture(wd, ht, pTextureOptions);
	}
	
	public void load()
	{
		mEngine.getTextureManager().loadTexture(mTexture);
	}
	
	public void unload()
	{
		mEngine.getTextureManager().unloadTexture(mTexture);
	}
	
	public Texture getHandle()
	{
		return mTexture;
	}
	
	public void cleanUp()
	{
		if ( bCleanUpDone == false ) {
			if ( mTexture != null ) {
				mEngine.getTextureManager().unloadTexture(mTexture);
				mTexture.clearTextureSources();
				mTexture = null;
			}
		} else {
			bCleanUpDone = true;
		}
	}
	
	protected void finalize() throws Throwable
	{
		cleanUp();
	}
}

/* end */

